package com.example.express.aspect;

import com.example.express.annotation.RateLimit; // 导入自定义注解
import com.google.common.util.concurrent.RateLimiter;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class RateLimitAspect {
    private final RateLimiter rateLimiter = RateLimiter.create(5.0); // 每秒5个请求

    @Before("@annotation(rateLimit)") // 使用自定义注解
    public void rateLimit(RateLimit rateLimit) {
        if (!rateLimiter.tryAcquire()) {
            throw new RuntimeException("请求过于频繁，请稍后再试");
        }
    }
}